package com.wegis.demo.service.impl;

import com.wegis.demo.service.IFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * description: FileServiceImpl
 * date: 2021/7/25 0:27
 * author: asus
 * version: 1.0
 */
@Service
public class FileServiceImpl implements IFileService {
    @Override
    public Map<String, Object> uploadFile(MultipartFile file) {
        Map<String, Object> mapResult = new HashMap<>();
        String fileName = file.getOriginalFilename();
        String caselsh = fileName.substring(0,fileName.lastIndexOf("."));
        String fileTyle=fileName.substring(fileName.lastIndexOf("."),fileName.length());
        String filePath = "E:/java/demo/src/main/resources/static/";
        File dest = new File(filePath + caselsh + UUID.randomUUID().toString()+ fileTyle);
        try {
            file.transferTo(dest);

        } catch (Exception e) {
            mapResult.put("msg", "上传失败，请选择文件");
            mapResult.put("status", 201);
            return mapResult;
        }
        mapResult.put("msg", "上传成功");
        mapResult.put("status", 200);
        return mapResult;
    }
}
